import { network } from "../../utils/network.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 3,
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading({
      title: '加载中...',
    })

    var that = this
    console.log(options)
    wx.request({
      url: 'http://neteasemusic.leanapp.cn/album',
      data:{
        id:options.id
      },
      success: function (res) {
        console.log(res.data.songs)
        console.log(res.data.album)
        that.setData({
          songs: res.data.songs,
          album: res.data.album
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onplay:function(event){
    var that=this;
    var id = event.currentTarget.dataset.id
    network.getPlay({
      id: id,
      success: function (url) {
        that.setData({
          url: url
        })
        wx.playBackgroundAudio({
          dataUrl: url,
          title: event.currentTarget.dataset.title,
          coverImgUrl: event.currentTarget.dataset.coverImgUrl,
        });
        console.log("play")
      },
    })


    wx.getStorage({
      key: 'played',
      success: function (res) {
        that.setData({
          histories: res.data,
        })
      },
    })
    
    var app = getApp()
    app.globalData.musicid = id;
    getApp().globalData.isFM = false;
    var isExisted = false;
    for (var index = 0; index < getApp().globalData.playing.length; index++) {
      var savedid = getApp().globalData.playing[index];
      if (savedid.id == id) {
        getApp().globalData.playno = index
        isExisted = true
        break
      }
    }
    if (!isExisted) {
      getApp().globalData.playing.push({ id: id });
      getApp().globalData.playno = getApp().globalData.playing.length
    }
    var histories = that.data.histories;
    if (histories) {
      for (var index = 0; index < histories.length; index++) {
        var savedid = histories[index];
        if (savedid.id == id) {
          histories.isExisted = true;
          histories.splice(index, 1)
          break;
        }
      }
    }
    if (!histories) {
      histories = [];
    }
    id = parseInt(id)
    histories.push({ id: id });
    wx.setStorage({
      key: 'played',
      data: histories,
      success: function (res) {
        console.log("保存成功")
      }
    })
   },

  onAllplayEvent: function () {
    var ids = []
    for (var i = 0; i < this.data.songs.length; i++) {
      ids.push({id:this.data.songs[i].id})
      this.setData({
        ids: ids
      })
    }
    getApp().globalData.playing = ids,
    getApp().globalData.playno = 0
    console.log(getApp().globalData.playing)
    wx.navigateTo({
      url: '../detail/detail?id=' + ids[0].id +'&playnow=true',
    })
  }
})